package server;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class RequestProcessor  extends  Thread{

    private Socket socket;

    private Map<String,HttpServlet> servletMap=new HashMap<>();

    public RequestProcessor(Socket socket,Map<String,HttpServlet>servletMap){
        this.servletMap =servletMap;
        this.socket=socket;
    }


    @Override
    public void run() {
        //获取输入流
        InputStream inputStream = null;
        try {
            inputStream = socket.getInputStream();
            Request request=new Request(inputStream);
            Response response=new Response(socket.getOutputStream());
            //判断请求的是否是静态文件
            if (   servletMap.get(request.getUrl())==null){
                //servlet集合中没有对应的servlet 证明请求的是静态资源
                response.outputHtml(request.getUrl());
            }else {
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request, response);
            }
            response.outputHtml(request.getUrl());
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
